import { isVNode } from 'vue'
import notificationComponent from './Notification.vue'

import { h, render } from 'vue'

const MOUNT_COMPONENT_REF = 'el_component'
const COMPONENT_CONTAINER_SYMBOL = Symbol('el_component_container')

/**
 * 销毁组件实例对象
 * @param {*} ComponnetInstance 通过createComponent方法得到的组件实例对象
 */
export function unmountComponent(ComponnetInstance) {
  render(undefined, ComponnetInstance.vnode[COMPONENT_CONTAINER_SYMBOL])
}

/**
 * 创建组件实例对象
 * 返回的实例和调用 getCurrentComponent() 返回的一致
 * @param {*} Component
 */
function createComponent(Component, props, children) {
  const vnode = h(Component, { ...props, ref: MOUNT_COMPONENT_REF }, children)
  const container = document.createElement('div')
  vnode[COMPONENT_CONTAINER_SYMBOL] = container
  render(vnode, container)
  return vnode.component
}
export function Notification(options) {
  return createNotification(options)
}
function createNotification(options) {
  const instance = createNotificationByOpts(options)
  return instance.proxy
}

function createNotificationByOpts(opts) {
  if (isVNode(opts.message)) {
    return createComponent(notificationComponent, opts, () => opts.message)
  }

  return createComponent(notificationComponent, opts)
}
